package com.example.no1admin.controller;

import com.example.no1admin.pojo.dto.album.AlbumAddNewDTO;
import com.example.no1admin.pojo.dto.album.AlbumEditDTO;
import com.example.no1admin.pojo.vo.album.AlbumListItemVO;
import com.example.no1admin.service.IAlbumService;
import com.example.no1admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "相册管理模块")
@RequestMapping("/albums")
@Slf4j
@RestController
public class AlbumController {
    @Autowired
    IAlbumService albumService;

    @ApiOperation("创建相册")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody AlbumAddNewDTO albumAddNewDTO){
        log.debug("接受到的请求参数:{}",albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除相册")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete-by-id")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("接收到删除相册的请求");
        albumService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改相册")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult updateById(@RequestBody AlbumEditDTO albumEditDTO){
        log.debug("接收到修改相册的请求");
        albumService.updateById(albumEditDTO);
        return JsonResult.ok();
    }

    @ApiOperation("相册列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询相册列表的请求");
        List<AlbumListItemVO> albums = albumService.list();
        return JsonResult.ok(albums);
    }

    @ApiOperation("相册列表")
    @ApiOperationSupport(order = 400)
    @GetMapping ("/list-By-Name")
    public JsonResult listByName(String name){
        log.debug("接收到根据名字查询相册列表的请求{}",name);
        List<AlbumListItemVO> albums = albumService.listByName(name);
        return JsonResult.ok(albums);
    }
}
